package cn.qyl.joha.utils;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.UUID;

import org.springframework.web.multipart.MultipartFile;

import cn.qyl.joha.common.PageConstant;


public class FilePath {
	public static String saveFile(MultipartFile file) throws IllegalStateException, IOException{
		//文件上传
		String serverUploadPath = PageConstant.FILE_PATH;
		//根据uuid生成文件名
		Serializable uuid = UUID.randomUUID();
		//获取文件名
		String fileName = file.getOriginalFilename();
		//根据.分割文件名
		String[] nameArray = fileName.split("\\.");
		String suffix = "";
		if(nameArray.length>1){
			//获取文件名后缀
			suffix = nameArray[1];
		}
		//生成新的名字 UUID + . +后缀 即uuid.jpg
		StringBuilder finallyName = new StringBuilder();
		finallyName.append(uuid+"."+suffix);
		File imgeFile = new File(serverUploadPath,finallyName.toString());
		if(!imgeFile.exists()){
			imgeFile.mkdirs();
		}
		//把表单传递过来的file进行转储到指定的服务器位置
		file.transferTo(imgeFile);
		return finallyName.toString();
	}

}
